package Forms;

import BusinessLogic.BL_Usuario;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;

/**
 *
 * @author Kenjy
 */
public class Frm_login extends javax.swing.JFrame {
    

    /** Creates new form Frm_login */
    public Frm_login() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        btnIngresar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/logo_llave.png"))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Usuario:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Contraseña:");

        btnIngresar.setText("Ingresar");
        btnIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/logo_calidad_2.png"))); // NOI18N

        txtPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPasswordActionPerformed(evt);
            }
        });
        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPasswordKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnIngresar)
                            .addComponent(btnCancelar)))
                    .addComponent(jLabel2))
                .addGap(144, 144, 144))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-621)/2, (screenSize.height-302)/2, 621, 302);
    }// </editor-fold>//GEN-END:initComponents

    private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresarActionPerformed
        try {
                ingresar();
        } catch (InstantiationException ex) {
            Logger.getLogger(Frm_login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Frm_login.class.getName()).log(Level.SEVERE, null, ex);
        }catch (SQLException ex) {
                Logger.getLogger(Frm_login.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnIngresarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void txtPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPasswordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPasswordActionPerformed

    private void txtPasswordKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyTyped
        if(evt.getKeyChar()==java.awt.event.KeyEvent.VK_ENTER){
                    btnIngresar.doClick();
         }
    }//GEN-LAST:event_txtPasswordKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                int skin = 1;
                int subskin = 15;
                switch (skin) {
                    case 0:
                        setLookAndFeel("quaquaMAC");
                        break;
                    case 1:
                        setLookAndFeel("liquidlnfMAC");
                        break;
                    case 2:
                        setLookAndFeel("OfficeLnFs", subskin);
                        break;
                    case 3:
                        setLookAndFeel("substance", subskin);
                        break;
                }
                new Frm_login().setVisible(true);
            }

            private void setLookAndFeel(String strSkin) {
                setLookAndFeel(strSkin, 0);
            }

            private void setLookAndFeel(String strSkin, int intTipoSkin) {

                try {
                    if (strSkin.equals("quaquaMAC")) {
                        UIManager.setLookAndFeel("ch.randelshofer.quaqua.tiger.Quaqua14TigerCrossPlatformLookAndFeel");
                    }
                    if (strSkin.equals("liquidlnfMAC")) { 
                        UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
                        com.birosoft.liquid.LiquidLookAndFeel.setLiquidDecorations(true, "mac");
                        com.birosoft.liquid.LiquidLookAndFeel.setStipples(false);
                    }
                    if (strSkin.equals("OfficeLnFs")) {
                        String strLib = "org.fife.plaf.";
                        switch ((intTipoSkin < 0) ? (0) : ((intTipoSkin > 2) ? (2) : (intTipoSkin))) {
                            case 0:
                                UIManager.setLookAndFeel(strLib + "VisualStudio2005.VisualStudio2005LookAndFeel");
                                break;
                            case 1:
                                UIManager.setLookAndFeel(strLib + "OfficeXP.OfficeXPLookAndFeel");
                                break;
                            case 2:
                                UIManager.setLookAndFeel(strLib + "Office2003.Office2003LookAndFeel");
                                break;
                        }
                    }
                    if (strSkin.equals("substance")) {
                        String strLib = "org.jvnet.substance.skin.Substance";
                        switch ((intTipoSkin < 0) ? (0) : ((intTipoSkin > 20) ? (20) : (intTipoSkin))) {
                            case 0:
                                UIManager.setLookAndFeel(strLib + "AutumnLookAndFeel");
                                break;
                            case 1:
                                UIManager.setLookAndFeel(strLib + "BusinessBlackSteelLookAndFeel");
                                break;
                            case 2:
                                UIManager.setLookAndFeel(strLib + "BusinessBlueSteelLookAndFeel");
                                break;
                            case 3:
                                UIManager.setLookAndFeel(strLib + "ChallengerDeepLookAndFeel");
                                break;
                            case 4:
                                UIManager.setLookAndFeel(strLib + "CremeCoffeeLookAndFeel");
                                break;
                            case 5:
                                UIManager.setLookAndFeel(strLib + "CremeLookAndFeel");
                                break;
                            case 6:
                                UIManager.setLookAndFeel(strLib + "DustCoffeeLookAndFeel");
                                break;
                            case 7:
                                UIManager.setLookAndFeel(strLib + "EmeraldDuskLookAndFeel");
                                break;
                            case 8:
                                UIManager.setLookAndFeel(strLib + "MagmaLookAndFeel");
                                break;
                            case 9:
                                UIManager.setLookAndFeel(strLib + "MagmaLookAndFeel");
                                break;
                            case 10:
                                UIManager.setLookAndFeel(strLib + "MistAquaLookAndFeel");
                                break;
                            case 11:
                                UIManager.setLookAndFeel(strLib + "MistSilverLookAndFeel");
                                break;
                            case 12:
                                UIManager.setLookAndFeel(strLib + "ModerateLookAndFeel");
                                break;
                            case 13:
                                UIManager.setLookAndFeel(strLib + "NebulaBrickWallLookAndFeel");
                                break;
                            case 14:
                                UIManager.setLookAndFeel(strLib + "OfficeBlue2007LookAndFeel");
                                break;
                            case 15:
                                UIManager.setLookAndFeel(strLib + "OfficeSilver2007LookAndFeel");
                                break;
                            case 16:
                                UIManager.setLookAndFeel(strLib + "RavenGraphiteGlassLookAndFeel");
                                break;
                            case 17:
                                UIManager.setLookAndFeel(strLib + "RavenGraphiteLookAndFeel");
                                break;
                            case 18:
                                UIManager.setLookAndFeel(strLib + "RavenLookAndFeel");
                                break;
                            case 19:
                                UIManager.setLookAndFeel(strLib + "SaharaLookAndFeel");
                                break;
                            case 20:
                                UIManager.setLookAndFeel(strLib + "TwilightLookAndFeel");
                                break;
                        }
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        });
    }

    private void limpiar() {
        this.txtUsuario.setText("");
        this.txtPassword.setText("");
        txtUsuario.requestFocus();
    }

    private void ingresar() throws InstantiationException, IllegalAccessException, SQLException {

        BL_Usuario objBLusuario = new BL_Usuario();
        String strUsr = this.txtUsuario.getText();
        char[] arrCharPass = this.txtPassword.getPassword();
        if (!strUsr.equals("") && strUsr != null && arrCharPass.length != 0) {
            String strPass = new String(arrCharPass);
            setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
            boolean puedePasar = objBLusuario.loginUser(strUsr, strPass);
            setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            if (puedePasar) {
                this.setVisible(false);
                Frm_principal frmVentanaPrincipal = new Frm_principal(strUsr);
                frmVentanaPrincipal.setVisible(true);
                this.dispose();
                
            } else {
                Comun.GUI.msjAlerta("Usuario o password no válido");
                limpiar();
            }
        } else {
            Comun.GUI.msjAlerta("Ingrese un usuario o password");
            limpiar();
        }
      
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnIngresar;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
